console.log('启动演练', $event);
const helper = new Helper(this);
// 变量
const params = helper.parseVariable('screen_params');
const prefix = helper.getVariable('environmentType');
const authToken = helper.getVariable('AuthToken');
// 组件
const $list = helper.get('list')?.widgetObj;
const search = helper.get('search')?.widgetObj;
// 服务
const eventRequest = EventServices.getInstance(prefix);

const startDrill = (drillPersonInfoDictTreeNodes, resData) => {
    if (!params?.id) return;

    const options = {
        ...params,
        emergencyPlan: JSON.stringify({
            emergencyResource: resData,
            eventId: params.eventId || params.id,
            planId: search.getFormData('form_1')?.name,
            eventLocation: params.drillLocation,
            eventNature: params.eventQualityCode,
            eventRegion: params.occurrencePlace,
            eventLevel: params.eventLevel,
            eventType: params.eventType,
        }),
        // 演练组织典信息
        drillPersonInfoDictTreeNodes,
        eventId: params.eventId || params.id,
        eventType: params.eventType,
        eventLevel: params.eventLevel,
        pointId: '',
        disableFlag: 2,
    };

    console.log('save: options => ', options);
    eventRequest.updateDrillPlan(options).then((result) => {
        console.log('save: options => ', result);
        if (result) {
            console.log('save  screen_params', options);
            window.layer?.msg('启动成功');
            this.getApiPage().navigateStage('843057919439142912',
                [
                    { name: 'screen_params', value: JSON.stringify(options) },
                    { name: 'environmentType', value: prefix },
                    { name: 'AuthToken', value: authToken },
                ])
        } else {
            window.layer?.msg('启动失败！');
        }
    });
}

function hasRooter ({ drillRoleCode, drillRole } = {}) {
    return drillRoleCode === '1720262536462589954' || drillRole === '总指挥'
}

function hasGrouper ({ drillRoleCode, drillRole } = {}) {
    return drillRoleCode === '7cd6254d-6e66-45a6-ab61-de17eebff425'
        || drillRole === '组长'
}

function findPerson(personTree) {
    return personTree.drillPersonInfos.find(v => v);
}

function findRooter(personTree) {
    return personTree.drillPersonInfos.find(hasRooter)
}

function findGrouper(personTree) {
    return personTree.drillPersonInfos.find(hasGrouper)
}

function findDrillRoot(personTree, findRole) {
    let role;
    if (Array.isArray(personTree)) {
        role = personTree.find(person => findDrillRoot(person, findRole))
        if (role) return role;
    }

    if (personTree?.drillPersonInfos?.length) {
        role = findRole(personTree);
        if (role) return role;
    }

    if (personTree?.drillPersonInfoDictTreeNodes?.length) {
        role = findDrillRoot(personTree.drillPersonInfoDictTreeNodes, findRole)
        if (role) return role;
    }
}

if ($list) {
    // 从启动演练的列表中，获取演练数据，排除应急小组
    const { emergencyGroup, ...resData } = $list?.getStartUpParams();
    const personTree = $list.getGroupData()

    console.log('verify', { emergencyGroup, ...resData });
    if (personTree?.length) {
        const persons = findDrillRoot(personTree, findPerson)
        if (persons) {
            const rooter = findDrillRoot(personTree, findRooter)
            if (rooter) {
                let group;
                const noGrouper = findDrillRoot(personTree, (tree) => {
                    if (tree.dictCode !== 'emergency_drill_headquarters') { // 排除指挥部
                        if (!findGrouper(tree)) {
                            // 如果这个组不存在
                            return group = tree;
                        }
                    }
                })
                if (noGrouper) {
                    window.layer?.msg(`"${group.dictName}"未选择组长，请选择组长！`)
                } else {
                    startDrill (personTree, resData)
                }
            } else {
                window.layer?.msg('请选择总指挥！')
            }
        } else {
            window.layer?.msg('请选择演练人员！')
        }
    } else {
        window.layer?.msg('请添加演练组织！')
    }
}
